<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<head>
</head>

<body class="no-skin">
	<table id="sample-table-1"
		class="table table-striped table-bordered table-hover">

		<tr>
			<a title="增加科目" class="btn btn-info btn-lg addSubject"> <span
				class="glyphicon glyphicon-plus-sign"></span></a>
		</tr>
		<tr>
			<th>科目ID</th>
			<th>科目</th>
			<th>简介</th>
			<th>考试次数</th>
			<th>考试时长(分钟)</th>
			<th>考试总分</th>
			<th>状态</th>
			<th>操作</th>
		</tr>
		<c:forEach items="${subjectList }" var="subjectList">
			<tr>
				<td>${subjectList.sub_id }</td>
				<td>${subjectList.name }</td>
				<td width="200" style="line-height: 20px;">${subjectList.description }</td>
				<td>${subjectList.test_count }</td>
				<td>${subjectList.total_time }</td>
				<td>${subjectList.total_score }</td>
				<td><c:choose>
						<c:when test="${subjectList.status==0 }">
							<input name="switch-field-1" subid="${subjectList.sub_id }"
								class="ace ace-switch ace-switch-5" type="checkbox" />
							<samp class="lbl"></samp>
						</c:when>
						<c:otherwise>
							<input name="switch-field-1" subid="${subjectList.sub_id }"
								checked class="ace ace-switch ace-switch-5" type="checkbox" />
							<samp class="lbl"></samp>
						</c:otherwise>
					</c:choose></td>
				<td><a title="修改科目" class="btn btn-primary btn-xs edit-subject"><i
						class="ace-icon fa fa-pencil"></i></a> <a
					href="deleteSubject/${subjectList.sub_id }" title="删除科目"
					class="btn btn-primary btn-xs delete-test" onclick="msg()"><i
						class="ace-icon fa fa-trash-o"></i></a> <a title="管理科目学生"
					class="btn btn-primary btn-xs manage-subject-student-btn"><i
						class="ace-icon fa fa-users"></i></a></td>
			</tr>
		</c:forEach>
	</table>

	<!-- 模态框（Modal）增加 -->
	<div class="modal fade" id="addSubjectModal" tabindex="-1"
		role="dialog" aria-hidden="true">
		<div class="modal-dialog">
			<div class="modal-content">
				<div class="modal-header">
					<button type="button" class="close" data-dismiss="modal"
						aria-hidden="true">&times;</button>
					<h4 class="modal-title">增加试题</h4>
				</div>
				<div class="modal-body">
					<form:form id="addSubjectForm">
						<table align="center">
							<tr>
								<td>科目</td>
								<td><input type="text" name="name" /></td>
							</tr>
							<tr>
								<td>简介</td>
								<td><input type="text" name="description" /></td>
							</tr>
							<tr>
								<td>考试次数</td>
								<td><input type="text" name=test_count /></td>
							</tr>
							<tr>
								<td>考试时长(分钟)</td>
								<td><input type="text" name="total_time" /></td>
							</tr>
							<tr>
								<td>考试总分</td>
								<td><input type="text" name="total_score" /></td>
							</tr>
							<tr>
								<td>状态</td>
								<td>关<input type="radio" checked="checked" name="status"
									value="0" /> 开<input type="radio" name="status" value="1" /></td>
							</tr>
						</table>
					</form:form>
				</div>
				<div class="modal-footer">
					<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
					<button id="addSubjectSaveBtn" type="button"
						class="btn btn-primary">保存</button>
				</div>
			</div>
			<!-- /.modal-content -->
		</div>
	</div>

	<!-- 模态框（Modal）修改 -->
	<div class="modal fade" id="editSubjectModal" tabindex="-1"
		role="dialog" aria-hidden="true">
		<div class="modal-dialog">
			<div class="modal-content">
				<div class="modal-header">
					<button type="button" class="close" data-dismiss="modal"
						aria-hidden="true">&times;</button>
					<h4 class="modal-title">修改试题</h4>
				</div>
				<div class="modal-body">
					<form:form id="editSubjectForm">
						<table align="center">
							<tr>
								<td><input type="hidden" name="subjectId" /></td>
							</tr>
							<tr>
								<td>科目</td>
								<td><input type="text" name="name" /></td>
							</tr>
							<tr>
								<td>简介</td>
								<td><input type="text" name="description" /></td>
							</tr>
							<tr>
								<td>考试次数</td>
								<td><input type="text" name="test_count" /></td>
							</tr>
							<tr>
								<td>考试时长(分钟)</td>
								<td><input type="text" name="total_time" /></td>
							</tr>
							<tr>
								<td>考试总分</td>
								<td><input type="text" name="total_score" /></td>
							</tr>
							<tr>
								<td>状态</td>
								<td><input class="sigle1" type="radio" name="status"
									value="0" />关 <input class="sigle1" type="radio" name="status"
									value="1" />开</td>
							</tr>
						</table>
					</form:form>
				</div>
				<div class="modal-footer">
					<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
					<button id="editSubjectSaveBtn" type="button"
						class="btn btn-primary">保存</button>
				</div>
			</div>
			<!-- /.modal-content -->
		</div>

	</div>

	<!-- 模态框（Modal）管理学生科目 -->
	<div class="modal fade" id="subjectStudentModal" tabindex="-1"
		role="dialog" aria-hidden="true">
		<div class="modal-dialog">
			<div class="modal-content">
				<div class="modal-header">
					<button type="button" class="close" data-dismiss="modal"
						aria-hidden="true">&times;</button>
					<h4 class="modal-title">
						管理学生科目:<label id="modalSubjectName"></label>
					</h4>
				</div>
				<div class="modal-body">
					<input type="hidden" id="subjectId" />
					<div id="subjectStudentContent" class="control-group"></div>
				</div>
				<div class="modal-footer">
					<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
					<button id="subjectStudentSaveBtn" type="button"
						class="btn btn-primary">保存</button>
				</div>
			</div>
			<!-- /.modal-content -->
		</div>

	</div>

	<!--[if !IE]> -->
	<script type="text/javascript">
		window.jQuery
				|| document
						.write("<script src='${pageContext.request.contextPath}/static/assets/js/jquery.min.js'>"
								+ "<"+"/script>");
	</script>

	<!-- inline scripts related to this page -->
	<script type="text/javascript">
		/* $().ready(function() {
			var validate = $("#addSubjectForm").validate({
				rules : {
					name : {
						required : true
					},
					description : {
						required : true
					},
					test_count : {
						required : true
					},
					total_time : {
						required : true
					},
					total_score : {
						required : true
					}
				},
				messages : {
					name : {
						required : "必填"
					},
					description : {
						required : "必填",
					},
					test_count : {
						required : "不能为空"
					},
					total_time : {
						required : "不能为空"
					},
					total_score : {
						required : "不能为空"
					}
				}
			});
		}) */

		jQuery(function($) {
			$('.edit-subject').click(
					function() {
						var sub_id = $(this).parent().siblings().eq(0).text();
						$.ajax({
							type : 'POST',
							url : '${contextPath}/teacher/getSubjectById',
							data : {
								sub_id : sub_id
							},
							dateType : "json",
							success : function(data) {
								$('#editSubjectForm input[name="subjectId"]')
										.val(sub_id);
								$('#editSubjectForm input[name="name"]').val(
										data.name);
								$('#editSubjectForm input[name="description"]')
										.val(data.description);
								$('#editSubjectForm input[name="test_count"]')
										.val(data.test_count);
								$('#editSubjectForm input[name="total_time"]')
										.val(data.total_time);
								$('#editSubjectForm input[name="total_score"]')
										.val(data.total_score);
								$("#editSubjectForm input[name='status'][value='"+data.status+"']")
										.prop("checked","checked"); 
							}
						});
						$('#editSubjectModal').modal('show');
					});

			$('#editSubjectSaveBtn')
					.click(
							function() {
								var sub_id = $(
										'#editSubjectForm input[name="subjectId"]')
										.val();
								var name = $(
										'#editSubjectForm input[name="name"]')
										.val();
								var description = $(
										'#editSubjectForm input[name="description"]')
										.val();
								var test_count = $(
										'#editSubjectForm input[name="test_count"]')
										.val();
								var totalTime = $(
										'#editSubjectForm input[name="total_time"]')
										.val();
								var totalScore = $(
										'#editSubjectForm input[name="total_score"]')
										.val();
								var status = $(
										'#editSubjectForm input[name="status"]:checked')
										.val();
								$.ajax({
									type : 'POST',
									url : '${contextPath}/teacher/editSubject',
									data : {
										sub_id : sub_id,
										name : name,
										description : description,
										test_count : test_count,
										total_time : totalTime,
										total_score : totalScore,
										status : status
									},
									dataType : "json",
									success : function(data) {
										if (data.success) {
											alert("修改成功");
											window.location.reload();
										}
									}
								});
							});

			$('.addSubject').click(function() {
				$('#addSubjectModal').modal('show');
			});

			$('#addSubjectSaveBtn').click(
					function() {
						var name = $('#addSubjectForm input[name="name"]')
								.val();
						var description = $(
								'#addSubjectForm input[name="description"]')
								.val();
						var test_count = $(
								'#addSubjectForm input[name="test_count"]')
								.val();
						var totalTime = $(
								'#addSubjectForm input[name="total_time"]')
								.val();
						var totalScore = $(
								'#addSubjectForm input[name="total_score"]')
								.val();
						var status = $(
								'#addSubjectForm input[name="status"]:checked')
								.val();
						$.ajax({
							type : 'POST',
							url : '${contextPath}/teacher/saveSubject',
							data : {
								name : name,
								description : description,
								test_count : test_count,
								total_time : totalTime,
								total_score : totalScore,
								status : status
							},
							dataType : "json",
							success : function(data) {
								if (data.success) {
									alert("添加成功");
									window.location.reload();
								}
							}
						});
					});

			//点击管理学生科目按钮，读取所有学生姓名和学号
			$('.manage-subject-student-btn')
					.click(
							function() {
								var subId = $(this).parent().siblings().eq(0)
										.text();
								$
										.ajax({
											type : 'POST',
											url : '${contextPath}/teacher/getAllStudent',
											data : {
												subjectId : subId
											},
											dateType : "json",
											success : function(data) {
												var htmlString = '';
												var selectedUser = data.selectedUser;
												//得到所有这个科目关联的学生
												for (var i = 0; i < data.allUser.length; i++) {
													var user = data.allUser[i];
													var checked = checkSelectedUser(
															selectedUser,
															user.user_id) ? ' checked="checked"'
															: ' ';
													htmlString += '<div class"checkox">';
													htmlString += '		<label>';
													htmlString += '			<input name = "subjectStudents" class="ace ace-checkbox-2" type="checkbox" value="'+ user.user_id + '"' + checked +'/>';
													htmlString += '			<span class="lbl"> '
															+ user.user_name
															+ '</span>';
													htmlString += '		</label>';
													htmlString += '</div>';
												}
												function checkSelectedUser(a, b) {
													for (var j = 0; j < a.length; j++) {
														if (a[j] == b)
															return true;
													}
													return false;
												}
												$('#subjectStudentContent')
														.html(htmlString);
												$('#subjectId').val(subId);
											}
										});
								$('#modalSubjectName').text(
										$(this).parents("td").siblings("td")
												.eq(1).text());
								$('#subjectStudentModal').modal('show');

							});

			//修改学生的勾选后，点击保存按钮
			$('#subjectStudentSaveBtn')
					.click(
							function() {
								var subjectStudents = [];
								for (var i = 0; i < $('input[name="subjectStudents"]:checked').length; i++) {
									subjectStudents[i] = $(
											$('input[name="subjectStudents"]:checked')[i])
											.val();
								}
								$.ajax({
									type:'POST',
									url : '${contextPath}/teacher/saveSubjectStudents',
									data : {
										subjectStudents :subjectStudents,
										subjectId: $('#subjectId').val()
									},
									dateType : "json",
									success : function(data) {
										alert('关联成功');
										$('#subjectStudentModal').modal('hide');
									}
								})
								
							});
		})

		function msg() {
			if (!confirm("你确认要删除吗？"))
				window.event.returnValue = false;
		}
	</script>
</body>
</html>
